#ifndef __GRAPHICS_H__
#define __GRAPHICS_H__

#include "../PreHeader.h"
#include "../PreDefine.h"
#include "Texture.h"
#include "Rect.h"

#define ANGEL_90	(65536 / 4)
#define ANGEL_180	(65536 / 2)
#define ANGEL_270	(65536 / 4 * 3)

#define COLOR_RED	0xFFFF0000
#define COLOR_GREEN	0xFF00FF00
#define COLOR_BLUE	0xFF0000FF
#define COLOR_BLACK	0xFF000000	
#define COLOR_WHITE	0xFFFFFFFF

#define VERTICES_NUM 256*10
#define MAX_OBJ_NUM_PER_FRAME VERTICES_NUM /4

typedef struct {
	GLfloat v[VERTICES_NUM*4*2]; //QuadsVerticesCoords
	GLfloat t[VERTICES_NUM*4]; //QuadsTextureCoords
	int		dx[VERTICES_NUM];
	int		dy[VERTICES_NUM];
	int		texName[MAX_OBJ_NUM_PER_FRAME];	
	//int		stateVector[MAX_OBJ_NUM_PER_FRAME];
	int		quadHead[MAX_OBJ_NUM_PER_FRAME];	
	int		count;
	int		objCount;
} ObjRenderQueueInfo;

extern ObjRenderQueueInfo gRenderQI;

enum{
	anchor_frm = 0,
	anchor_screen
};

enum{
	IS_NONE		= 1<<0,
	IS_ROT		= 1<<1,
	IS_SCALE	= 1<<2,
	IS_X		= 1<<3,
	IS_Y		= 1<<4,
	IS_Z		= 1<<5,
	IS_TOP		= 1<<6,
	IS_BOTTOM	= 1<<7,
	IS_HVCENTER	= 1<<8,
	IS_LEFT		= 1<<9,
	IS_RIGHT	= 1<<10,
	IS_MUTILFRAME = 1<<11,
	IS_UNIFORMCOLOR = 1<<12
};
#define IS_X_ROT (IS_ROT|IS_X)
#define IS_Y_ROT (IS_ROT|IS_Y)
#define IS_Z_ROT (IS_ROT|IS_Z)
#define IS_X_ROT_TOP (IS_ROT|IS_X|IS_TOP)
#define IS_X_ROT_BOTTOM (IS_ROT|IS_X|IS_BOTTOM)

#define Is_FrameCenter (IS_HVCENTER)
#define Is_Center_LT2HV (IS_TOP|IS_LEFT|IS_HVCENTER)
#define Is_MultiFrameCenter (IS_HVCENTER|IS_MUTILFRAME)

typedef struct {
	float			destX;
	float			destY;
	float			destZ;
	int			modX;
	int			modY;
	int			modW;
	int			modH;
	int			oX_is; //sprite original position in screen
	int			oY_is;
	int			oX_if; //sprite original position in frame
	int			oY_if;
	GLfloat		scaleX;
	GLfloat		scaleY;
	GLfloat		rot;
	int			transform;
	int			anchorType;//coordinateSys;
	//int			texIdx;
	TexInfo *	tex;
	//GLint		stateVector;
	//int			ShaderInfoId;
	//GLboolean	bUniformColor;
}ObjCommonRenderInfo; //sprite, texture in 3D

//typedef struct {
//	int destX;
//	int destY;
//	int destZ;
//	int modX;
//	int modY;
//	int modW;
//	int modH;
//	int isXYZ;
//	T_F32 rot;	
//	T_F32 scaleX;
//	T_F32 scaleY;
//}Tex3DRenderInfo;

//static const GLfloat CAMERA_FOVY;
const T_F32 G_CAMERA_ASPECT = 320.f/240.f;

//extern GLfloat g3DViewbase;
//static const GLfloat CAMERA_NEARZ;
//static const GLfloat CAMERA_FARZ;


	void GFX_Initialize();
	void GFX_Clear(int col);

	void GFX_SetClip(int x, int y, int width, int heigth);
	void GFX_ResetClip();
	void GFX_SetLastClip();
	void GFX_ClipRect(int x, int y, int width, int height);

	void GFX_ResetSprCommRI();
	void GFX_SetScale(T_F32 x, T_F32 y, int ax = MAGIC_NUM, int ay = MAGIC_NUM);
	void GFX_ResetScale();
	void GFX_SetRotate(T_F32 angle, char isXYZ = IS_Z_ROT);
	void GFX_ResetRotate();

#ifdef _OptimizePaintFrame
#else
	void GFX_AdjustAnchor(int ax, int ay);
	void GFX_AdjustAnchor(int anchorType = Is_FrameCenter);
	void GFX_SetAnchor(int ax, int ay, int type = anchor_screen);
	void GFX_ClearAnchor(int type = anchor_screen);
#endif

void GFX_2DBeginRendering();
void GFX_2DEndRendering();
void GFX_3DBeginRendering();
void GFX_3DEndRendering();

extern ObjCommonRenderInfo g2DCommRI;
extern ObjCommonRenderInfo g3DCommRI;

void GFX_3DSetClipPlane(bool enable, GLfloat value = 0.0f);

int GFX_BlendColors(int c1, int c2, int blend);


void Begin3D(bool _flipOrthoX = false);
void End3D();

#endif
